﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace KidsProtector
{
    public partial class CreateUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            nameEmpty.Visible = false;
            mailEmpty.Visible = false;
            passEmpty.Visible = false;
            created.Visible = false;
            birthdayError.Visible = false;
            if (!IsPostBack)
            {
                List<int> lyear = new List<int>();
                List<int> lmonth = new List<int>();
                List<int> lday = new List<int>();
                for (int i = 0; i < 80; i++)
                    lyear.Add(DateTime.Now.Year - i);
                for (int i = 1; i < 13; i++)
                    lmonth.Add(i);
                for (int i = 1; i < 32; i++)
                    lday.Add(i);
                day.DataSource = lday;
                day.DataBind();
                day.Rows = 1;
                month.DataSource = lmonth;
                month.DataBind();
                month.Rows = 1;
                year.DataSource = lyear;
                year.DataBind();
                year.Rows = 1;
                day.SelectedIndex = DateTime.Now.Day - 1;
                month.SelectedIndex = DateTime.Now.Month - 1;
            }
        }

        public void ValidateOnClick(Object sender, EventArgs e)
        {
            bool ok = true;
            if (name.Text == "")
            {
                nameEmpty.Visible = true;
                ok = false;
            }
            if (mail.Text == "")
            {
                mailEmpty.Visible = true;
                ok = false;
            }
            if (pass.Text == "")
            {
                passEmpty.Visible = true;
                ok = false;
            }

            ok = testDate(day.SelectedValue, month.SelectedValue, year.SelectedValue) && ok;

            if (ok)
            {
                created.Visible = true;
                DateTime bday = new DateTime(int.Parse(year.SelectedValue), int.Parse(month.SelectedValue),int.Parse(day.SelectedValue));
                BusinessManagement.User bm = new BusinessManagement.User();
                bm.CreateUser(name.Text, mail.Text, pass.Text, bday);
                Response.Redirect("Download.aspx");
            }
                
        }
        private bool testDate(string sday, string smonth, string syear)
        {
            int iday = int.Parse(sday);
            int imonth = int.Parse(smonth);
            int iyear = int.Parse(syear);
            
            if (iday < 29 || imonth == 1 || imonth == 3 || imonth == 5 || imonth == 7 || imonth == 8 || imonth == 10 || imonth == 12)
                return true;
            if (iday <= 30 && imonth != 2)
                return true;
            if (iday == 29 && ((imonth % 4 == 0  && imonth % 100 != 0) || imonth % 400 == 0))
                return true;
            birthdayError.Visible = true;
            return false;
        }
    }
}